//
//  ReplyComtView.m
//  JiaoX
//
//  Created by yinqy on 15/1/9.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ReComtView.h"
#import "ReComtItemView.h"
#import "JXComtPopMenu.h"
#import "JXNavigationController.h"
#import "JXLoginRegistCheckController.h"
@interface ReComtView()<JXComtPopMenuDelegate>{
    NSMutableArray *cacheView;
    NSMutableArray *useView;
    CGFloat _width;
    JXComtPopMenu *_menu;
}

@end

@implementation ReComtView

-(instancetype)initWithWidth:(CGFloat)width{
    self = [super init];
    if (self) {
        _width = width;
        cacheView = [NSMutableArray array];
        useView = [NSMutableArray array];
    }
    return self;
}

-(void)setReComtArray:(NSArray *)reComtArray{
    _reComtArray = reComtArray;
    [cacheView addObjectsFromArray:useView];
    [useView removeAllObjects];
    //移除所有的子view
    [cacheView makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if(reComtArray==nil || reComtArray.count==0){
        [self setHeight:0];
        return;
    }
    for (int i = 0; i < reComtArray.count; i++) {
        ReComtItemView *view = [self getView];
        [view setTag:i];
        [view setComt:reComtArray[i]];
        CGFloat y = 0;
        if(useView.count){
            ReComtItemView *temp = useView.lastObject;
            y = CGRectGetMaxY(temp.frame);
        }
        view.x = 0;
        view.y = y;
        [useView addObject:view];
        [self addSubview:view];
        
        //添加点击事件
        
        [view setUserInteractionEnabled:YES];
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(childItemClick:)];
        [view addGestureRecognizer:singleTap];
    }
    ReComtItemView *temp = useView.lastObject;
    [self setSize:CGSizeMake(_width, CGRectGetMaxY(temp.frame))];
}


-(ReComtItemView *)getView{
    if(cacheView.count>0){
        ReComtItemView *view = cacheView.firstObject;
        [cacheView removeObjectAtIndex:0];
        return view;
    }else{
        ReComtItemView *view = [[ReComtItemView alloc]initWithWidth:_width];
        return view;
    }
}

+(CGFloat)getHeightWithComtArray:(NSArray *)comtArray width:(CGFloat)width{
    if(comtArray==nil || comtArray.count==0)
        return 0;
    CGFloat height = 0;
    for (int i=0; i<comtArray.count; i++) {
        CommentInfo *comt = comtArray[i];
        CGFloat tempH = [ReComtItemView getHeightWithComt:comt width:width];
        height = height + tempH;
    }
    return height;
}

-(void)childItemClick:(UITapGestureRecognizer *)grz{
    _menu = [[JXComtPopMenu alloc]initHaveArrow:YES];
    [_menu setTag:grz.view.tag];
    [_menu setDelegate:self];
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect rect=[grz.view convertRect:grz.view.bounds toView:window];
    [_menu showInPoint:CGPointMake(JXScreenW-15-_menu.container.width-[UIImage imageNamed:@"ic_comt_reply"].size.width, rect.origin.y-_menu.container.height)];
}
-(void)popMenuReplyBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(childViewClick:)]) {
        [_delegate childViewClick:btn.tag];
    }
}

-(void)popMenuChatBtnClick:(UIButton *)btn{
    if (!TOKEN) {
        JXNavigationController *nav = [[JXNavigationController alloc]initWithRootViewController:[[JXLoginRegistCheckController alloc]init]];
        [self.viewController presentViewController:nav animated:YES completion:^{}];
        return;
    }
    if ([self.delegate respondsToSelector:@selector(chatBtnClick:)]) {
        [_delegate chatBtnClick:btn.tag];
    }
}

//-(void)popMenuChatBtnClick:(UIButton *)btn{
//    if (!TOKEN) {
//        JXNavigationController *nav = [[JXNavigationController alloc]initWithRootViewController:[[JXLoginRegistCheckController alloc]init]];
//        [self.viewController presentViewController:nav animated:YES completion:^{}];
//        return;
//    }
//    
//    if ([self.delegate respondsToSelector:@selector(chatBtnClick:)]) {
//        [_delegate chatBtnClick:btn];
//    }
//}

@end
